var prefix="/menu/";
$(function () {
	    var obj=$("#menu_type");
		if(type==0){
			obj.text("目录");
			obj.addClass("label-primary");
		}else if(type==1){
			obj.text("菜单");
			obj.addClass("label-warning");
		}else{
			obj.addClass("label-success");
			obj.text("按钮");
		}
	valid();
	initClick();
	$("#optionsRadios"+type).click();
});
$.validator.setDefaults({
    submitHandler: function() {
    	update();
    }
});
function update(){
    var config={
			url : prefix+"/update",
			data:getFormJson("signupForm"),
	}
	EasyAjax.post_json(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
			window.parent.location.reload();
		} 
	});
}

function addIco() {
	layer.open({
		type : 2,
		title : '选择图标',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '850px', '680px' ],
		content : prefix + '/fontawesome/'
	});
}

function setIcon(css){
  $("#icon").val(css);
}


function initClick(){
	$("#optionsRadios0").click(function(){
		$("#menu_perms").hide();
		$("#menu_perms").find("input").val('');
		$("#menu_url").hide();
		$("#menu_url").find("input").val('');
	})
	$("#optionsRadios1").click(function(){
		$("#menu_perms").show();
		$("#menu_url").show();
	})
	$("#optionsRadios2").click(function(){
		$("#menu_perms").show();
		$("#menu_url").hide();
		$("#menu_url").find("input").val('');
	})
}
function valid(){
    var icon = "<i class='fa fa-times-circle'></i>";
    $("#signupForm").validate({
        rules:{
        	name: {
        		 required: true,
                 maxlength: 10,
        	},
        	type: {
                required: true,
            },
            perms:{
            	 required: true,
            	 notchcharacter:true
            },
            orderNum:{
            	number:true,
            	digits:true
            },
            url: {
                required: true,
                maxlength: 30,
                notchcharacter:true
            },
        },
        messages: {
        	name: {
        		required:icon + "请输入菜单名称",
        		maxlength:icon + "最多10个字符",
        	},
        	type: {
        		required:icon + "请选择菜单类型",
        	},
        	orderNum:{
             	number:icon + "请输入合法的顺序",
             	digits:icon + "请输入合法的顺序",
            },
        	url: {
        		required:icon + "请输角色标识",
        		maxlength:icon + "最多30个字符",
        		notchcharacter:icon + "不能输入汉字",
        	},
        	perms: {
        		required:icon + "请输入权限标识",
        		notchcharacter:icon + "不能输入汉字",
        	},
        	 
        }
    });
}

